package com.hbxs.folder.bean;

import cn.hutool.core.exceptions.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.validation.ConstraintViolationException;
import javax.xml.bind.ValidationException;

/**
 * @author radon
 * @ClassName: GlobalDefaultExceptionHandler
 * @Description: USEFULNESS(全局异常处理类)
 */
@ControllerAdvice
@ResponseBody
@Slf4j
public class GlobalDefaultExceptionHandler {
    
    /**
     * 自定义错误
     * @param c
     * @return
     */
    @ExceptionHandler(value = CustomGenericException.class)
    public Result<CustomGenericException> customGenericExceptionHnadler(CustomGenericException c){
       return new Result(c.getCode(),c.getMsg(),"");
    }

    /**
     * 全部错误
     * @param c
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public Result<String> allException(Exception e){
        log.error("服务错误:\r\n"+ExceptionUtil.getMessage(e));
        log.error("服务错误描述:\r\n"+ExceptionUtil.stacktraceToString(e));
        return Result.generate(Result.CodeList.SERVER_ERROR, ExceptionUtil.getMessage(e),ExceptionUtil.stacktraceToString(e));
    }

    /**
     * 方法参数校验
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result<String> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        return Result.generate(Result.CodeList.PARAMS_ERROR, e.getBindingResult().getFieldError().getDefaultMessage(),"");
    }

    /**
     * ValidationException
     */
    @ExceptionHandler(ValidationException.class)
    public Result<String> handleValidationException(ValidationException e) {
        return Result.generate(Result.CodeList.PARAMS_ERROR, e.getCause().getMessage(),"");
    }

    /**
     * ValidationException
     */
    @ExceptionHandler(BindException.class)
    public Result<String> handleBindException(BindException e) {
        return Result.generate(Result.CodeList.PARAMS_ERROR,e.getBindingResult().getFieldError().getDefaultMessage(),"");
    }

    /**
     * ConstraintViolationException
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public Result<String> handleConstraintViolationException(ConstraintViolationException e) {
        return Result.generate(Result.CodeList.PARAMS_ERROR, e.getMessage(),"");
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public Result<String> handlerNoFoundException(Exception e) {
        log.error(e.getMessage(), e);
        return Result.generate(Result.CodeList.NOT_FOUND, "路径不存在，请检查路径是否正确","");
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public Result<String> handleDuplicateKeyException(DuplicateKeyException e) {
        log.error(e.getMessage(), e);
        return Result.generate(Result.CodeList.PARAMS_ERROR, "数据重复，请检查后提交","");
    }

}
